package com.example.satoken.config;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author chenhh
 * @version v1.0
 * @date 2025/5/12 17:20
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    // 捕获权限异常
    @ExceptionHandler(NotLoginException.class)
    public Result handleNotLoginException(NotLoginException e) {

        return Result.error(401, e.getMessage());
    }

    @ExceptionHandler(NotPermissionException.class)
    public Result handleNotPermissionException(NotPermissionException e) {
        return Result.error(403, "无权限：" + e.getPermission());
    }

    @ExceptionHandler(NotRoleException.class)
    public Result handleNotRoleException(NotRoleException e) {
        return Result.error(403, "需要角色：" + e.getRole());
    }
}
